﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class EditEquityHeaderModel
    {
        public EquityHeader Header { get; set; }
        public EquityHeaderScreenData ScreenData { get; set; }
        public IList<EquityTransaction> Transactions
        {
            set
            {
                IList<EquityTransaction> data = value;
                if (data != null && data.Count > 0)
                {
                    foreach (var item in data)
                    {
                        TransactionAdaters.Add(new EquityTransactionAdapter(item, Header));
                    }
                }
            }
        }
        public IList<EquityPrice> Prices
        {
            set
            {
                IList<EquityPrice> data = value;
                if (data != null && data.Count > 0)
                {
                    if (PriceAdapter == null)
                        PriceAdapter = new List<EquityPriceAdapter>();
                    int i = 0;
                    foreach (var item in data)
                    {
                        PriceAdapter.Add(new EquityPriceAdapter(++i, item));
                    }
                }
            }
        }

        public IList<EquityPriceAdapter> PriceAdapter { get; set; }
        public IList<EquityTransactionAdapter> TransactionAdaters { get; set; }

        public EditEquityHeaderModel()
        {
            Init();
        }
        void Init()
        {
            if (null == Header)
                Header = new EquityHeader();
            if (TransactionAdaters == null)
                TransactionAdaters = new List<EquityTransactionAdapter>();

            var defaultItem = WebUtil.SelectListSelectDefault();
            CompanyList = new BindingList<SelectListItem>() { defaultItem };
            EquityTypeList = new BindingList<SelectListItem>() { defaultItem };
            SectorGroupList = new BindingList<SelectListItem>() { defaultItem };
            SectorList = new BindingList<SelectListItem>() { defaultItem };
            ListedStatusList = new BindingList<SelectListItem>() { defaultItem };
            //BrokerList = new BindingList<SelectListItem>() { defaultItem };
            InvolvedPartyTypeNameList = new BindingList<SelectListItem>() { defaultItem };
            RelatedOrNonRelatedList = new BindingList<SelectListItem>() { defaultItem };
        }
        public static EditEquityHeaderModel BuildModel(EquityHeaderScreenData data)
        {

            var model = new EditEquityHeaderModel();
            model.ScreenData = data;
            if (data.HeaderDTO != null)
            {
                if (data.HeaderDTO.Header != null)
                    model.Header = data.HeaderDTO.Header;
                if (data.HeaderDTO.Transactions != null)
                    model.Transactions = data.HeaderDTO.Transactions;
                if (data.HeaderDTO.Prices != null)
                    model.Prices = data.HeaderDTO.Prices;

                var involved = data.HeaderDTO.InvolvedPartyType;
                model.SelectedInvolvedPartyType = involved.InvolvedPartyTypeId;
                model.InvolvedPartyTypeNO = involved.InvolvedPartyTypeCode;
                model.InvolvedPartyTypeReferance = involved.InvolvedPartyTypeReference;
            }

            if (data.Companies != null || data.Companies.Any())
            {
                foreach (var selectListItem in data.Companies)
                {
                    model.CompanyList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.CompanyNameTh, Value = selectListItem.CompanyId.ToString() });
                }
            }
            if (data.EquityType != null || data.EquityType.Any())
            {
                foreach (var selectListItem in data.EquityType)
                {
                    model.EquityTypeList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.ValueEN, Value = selectListItem.ID.ToString() });
                }
            }
            if (data.ListStatus != null || data.ListStatus.Any())
            {
                foreach (var selectListItem in data.ListStatus)
                {
                    model.ListedStatusList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.ListStatusName, Value = selectListItem.ListStatusID.ToString() });
                }
            }
            if (data.InvolvedPartyTypes != null || data.InvolvedPartyTypes.Any())
            {
                foreach (var selectListItem in data.InvolvedPartyTypes)
                {
                    model.InvolvedPartyTypeNameList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.InvolvedPartyTypeName, Value = selectListItem.InvolvedPartyTypeId.ToString() });
                }
            }
            if (data.RelateType != null || data.RelateType.Any())
            {
                foreach (var selectListItem in data.RelateType)
                {
                    model.RelatedOrNonRelatedList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.ValueTH, Value = selectListItem.ID.ToString() });
                }
            }

            return model;
        }

        #region dropdown list

        [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedCompany
        {
            get { return Header.EquityCompanyID; }
            set { Header.EquityCompanyID = value; }
        }
        public IList<SelectListItem> CompanyList { get; set; }

        public int? SelectedEquityType
        {
            get { return Header.EquityTypeID; }
            set { Header.EquityTypeID = value; }
        }
        public IList<SelectListItem> EquityTypeList { get; set; }

        public string SelectedSectorGroup
        {
            get { return Header.EquitySectorGroup; }
            set { Header.EquitySectorGroup = value; }
        }
        public IList<SelectListItem> SectorGroupList { get; set; }

        public string SelectedSector
        {
            get { return Header.EquitySector; }
            set { Header.EquitySector = value; }
        }
        public IList<SelectListItem> SectorList { get; set; }

        public string SelectedListedStatus
        {
            get { return Header.ListedStatus; }
            set { Header.ListedStatus = value; }
        }
        public IList<SelectListItem> ListedStatusList { get; set; }

        //public int? SelectedBroker
        //{
        //    get { return Header.BrokerID; }
        //    set { Header.BrokerID = value; }
        //}
        //public IList<SelectListItem> BrokerList { get; set; }

        private int _involvedPartyTypeID;
        public int SelectedInvolvedPartyType
        {
            get { return _involvedPartyTypeID; }
            set
            {
                _involvedPartyTypeID = value;
            }
        }

        public IList<SelectListItem> InvolvedPartyTypeNameList { get; set; }

        //Related / Non Related
        public int? SelectedRelatedOrNonRelated
        {
            get { return Header.RelatedID; }
            set { Header.RelatedID = value; }
        }
        public IList<SelectListItem> RelatedOrNonRelatedList { get; set; }

        #endregion

        //#region adapter
        public int HeaderID
        {
            get
            {
                return Header.EquityHeaderID;
            }

            set
            {
                Header.EquityHeaderID = value;
            }
        }

        public string InvolvedPartyTypeNO { get; set; }
        public string InvolvedPartyTypeReferance { get; set; }

        [Required(ErrorMessage = "โปรดระบุ")]
        [StringLength(100, ErrorMessage="ความยาวไม่เกิน 100 ตัวอักษร")]
        public string Symbol
        {
            get { return Header.Symbol; }
            set { Header.Symbol = value; }
        }

        //public String InvolvedPartyTypeReferance { get; set; }

        private bool _isTierISelected;
        public bool IsTierISelected
        {
            get { return _isTierISelected; }
            set
            {
                _isTierISelected = value;
                //if (value)
                //{
                //    Header.LiquidityTierIPercent = "Tier I";
                //}
            }
        }

        private bool _isTierIiSelected;
        public bool IsTierIISelected
        {
            get { return _isTierIiSelected; }
            set
            {
                _isTierIiSelected = value;
                //if (value)
                //{
                //    Header.LiquidityName = "Tier II";
                //}
            }
        }

        private bool _isLowLiquiditySelected;
        public bool IsLowLiquiditySelected
        {
            get { return _isLowLiquiditySelected; }
            set
            {
                _isLowLiquiditySelected = value;
                //if (value)
                //{
                //    Header.LiquidityName = "Low Liquidity";
                //}
            }
        }


        //#endregion

        public String SaveBtn { get; set; }
        //public String CancelBtn { get; set; }
        //public String EditBtn { get; set; }
        public bool UpdateMode { get; set; }

        public decimal? BidAmount { get; set; }
        public decimal? BVAmount { get; set; }
        public decimal? FVAmount { get; set; }
        public decimal? CloseAmount { get; set; }
    }

}